<article class="" id="post-">
<header class="entry-header">
<h1 class="entry-title">.not()</h1>
<div class="entry-meta">
						所属分类：<span class="category"><a href="/category/traversing/">遍历</a> &gt; <a href="/category/traversing/filtering/">过滤</a></span><span class="category-divider"> | </span><span class="category"><a href="/category/traversing/">遍历</a> &gt; <a href="/category/traversing/miscellaneous-traversal/">其它遍历</a></span>
<span class="pull-right">英文文档：<a href="https://api.jquery123.com/not/" target="_blank">.not()</a></span>
</div><!-- .entry-meta -->
</header><!-- .entry-header -->
<!-- .entry-header -->
<div class="entry-content">
<article class="entry method" id="not1">
<h2 class="section-title"> <span class="name">.not( selector )</span><span class="returns">返回: <a href="/Types/#jQuery">jQuery</a></span> </h2>
<div class="entry-wrapper">
<p class="desc"><strong>描述: </strong>从匹配的元素集合中移除指定的元素。</p>
<ul class="signatures">
<li class="signature" id="not-selector">
<h4 class="name"> <span class="version-details">添加的版本: <a href="/category/version/1.0/">1.0</a></span>.not( selector )</h4>
<ul>
<li>
<div><strong>selector</strong></div>
<div>类型: <a href="/Types/#Selector">Selector</a> </div>
<div>一个用于匹配元素的选择器字符串。</div>
</li>
</ul>
</li>
<li class="signature" id="not-elements">
<h4 class="name"> <span class="version-details">添加的版本: <a href="/category/version/1.0/">1.0</a></span>.not( elements )</h4>
<ul>
<li>
<div><strong>elements</strong></div>
<div>类型: <a href="/Types/#Elements">Elements</a> </div>
<div>要从匹配元素集合中移除的一个或多个DOM元素。</div>
</li>
</ul>
</li>
<li class="signature" id="not-functionindex">
<h4 class="name"> <span class="version-details">添加的版本: <a href="/category/version/1.4/">1.4</a></span>.not( function(index) )</h4>
<ul>
<li>
<div><strong>function(index)</strong></div>
<div>类型: <a href="/Types/#Function">Function</a>()</div>
<div>一个函数用作测试集合中的每个元素。<code>this</code> 是当前DOM元素。</div>
</li>
</ul>
</li><li class="signature" id="not-jQuery-object">
<h4 class="name"> <span class="version-details">添加的版本: <a href="/category/version/1.4/">1.4</a></span>.not( jQuery object )</h4>
<ul>
<li>
<div><strong>jQuery object</strong></div>
<div>类型: <a href="/Types/#PlainObject">PlainObject</a> </div>
<div>现有匹配当前元素集合的jQuery对象。</div>
</li>
</ul>
</li>
</ul>
<div class="longdesc" id="entry-longdesc">
<p>如果提供的jQuery对象代表了一组DOM元素，<code>.not()</code>方法构建一个新的匹配元素的jQuery对象，用于存放筛选后的元素。所提供的选择器是对每个元素进行测试;如果元素不匹配的选择将包括在结果中。</p>
<p>考虑一个页面上一个简单的列表：</p>
<div class="syntaxhighlighter xml nogutter">
<table>
<tbody>
<tr>
<td class="gutter"><div class="line n1">1</div>
<div class="line n2">2</div>
<div class="line n3">3</div>
<div class="line n4">4</div>
<div class="line n5">5</div>
<div class="line n6">6</div>
<div class="line n7">7</div></td>
<td class="code"><pre><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">ul</span>&gt;</span></code></div></div><div class="container"><div class="line"><code> <span class="tag">&lt;<span class="title">li</span>&gt;</span>list item 1<span class="tag">&lt;/<span class="title">li</span>&gt;</span></code></div></div><div class="container"><div class="line"><code> <span class="tag">&lt;<span class="title">li</span>&gt;</span>list item 2<span class="tag">&lt;/<span class="title">li</span>&gt;</span></code></div></div><div class="container"><div class="line"><code> <span class="tag">&lt;<span class="title">li</span>&gt;</span>list item 3<span class="tag">&lt;/<span class="title">li</span>&gt;</span></code></div></div><div class="container"><div class="line"><code> <span class="tag">&lt;<span class="title">li</span>&gt;</span>list item 4<span class="tag">&lt;/<span class="title">li</span>&gt;</span></code></div></div><div class="container"><div class="line"><code> <span class="tag">&lt;<span class="title">li</span>&gt;</span>list item 5<span class="tag">&lt;/<span class="title">li</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;/<span class="title">ul</span>&gt;</span></code></div></div>
			</pre></td>
</tr>
</tbody>
</table>
</div>
<p>我们可以应用此方法来设置列表：</p>
<div class="syntaxhighlighter javascript nogutter">
<table>
<tbody>
<tr>
<td class="gutter"><div class="line n1">1</div></td>
<td class="code"><pre><div class="container"><div class="line"><code>$(<span class="string">'li'</span>).not(<span class="string">':even'</span>).css(<span class="string">'background-color'</span>, <span class="string">'red'</span>);</code></div></div>
			</pre></td>
</tr>
</tbody>
</table>
</div>
<p>此调用的结果是列表项2和4背景色变成红色，因为它们不匹配选择（记得:even 和 :odd使用基于0的索引）。</p>
<h4>Removing Specific Elements（删除特殊的元素）</h4>
<p>第二个版本<code>.not()</code>方法允许我们删除匹配的元素集合中元素。假设我们已经通过其他方式找到了一些元素。例如，假设我们有一个ID列表应用到它的项目之一：</p>
<div class="syntaxhighlighter xml nogutter">
<table>
<tbody>
<tr>
<td class="gutter"><div class="line n1">1</div>
<div class="line n2">2</div>
<div class="line n3">3</div>
<div class="line n4">4</div>
<div class="line n5">5</div>
<div class="line n6">6</div>
<div class="line n7">7</div></td>
<td class="code"><pre><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">ul</span>&gt;</span></code></div></div><div class="container"><div class="line"><code> <span class="tag">&lt;<span class="title">li</span>&gt;</span>list item 1<span class="tag">&lt;/<span class="title">li</span>&gt;</span></code></div></div><div class="container"><div class="line"><code> <span class="tag">&lt;<span class="title">li</span>&gt;</span>list item 2<span class="tag">&lt;/<span class="title">li</span>&gt;</span></code></div></div><div class="container"><div class="line"><code> <span class="tag">&lt;<span class="title">li</span> <span class="attribute">id</span>=<span class="value">"notli"</span>&gt;</span>list item 3<span class="tag">&lt;/<span class="title">li</span>&gt;</span></code></div></div><div class="container"><div class="line"><code> <span class="tag">&lt;<span class="title">li</span>&gt;</span>list item 4<span class="tag">&lt;/<span class="title">li</span>&gt;</span></code></div></div><div class="container"><div class="line"><code> <span class="tag">&lt;<span class="title">li</span>&gt;</span>list item 5<span class="tag">&lt;/<span class="title">li</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;/<span class="title">ul</span>&gt;</span></code></div></div>
			</pre></td>
</tr>
</tbody>
</table>
</div>
<p>我们可以使用原生的JavaScript <code>getElementById()</code>函数来获得它，然后将它从 jQuery 对象中移除掉：</p>
<div class="syntaxhighlighter javascript nogutter">
<table>
<tbody>
<tr>
<td class="gutter"><div class="line n1">1</div>
<div class="line n2">2</div></td>
<td class="code"><pre><div class="container"><div class="line"><code>$(<span class="string">'li'</span>).not(document.getElementById(<span class="string">'notli'</span>))</code></div></div><div class="container"><div class="line"><code> .css(<span class="string">'background-color'</span>, <span class="string">'red'</span>);</code></div></div>
			</pre></td>
</tr>
</tbody>
</table>
</div>
<p>述语句会改变第 1, 2, 4 和 5 列表项的背景色。我们可以用一个简单的jQuery表达式完成同样的事情，但这种技术有时候可能很有用，例如，我们还使用了其它返回结果是 DOM 节点的 JavaScript 库的话。</p>
<p>从jQuery 1.4开始，<code>.not()</code>方法可以接受一个函数作为参数，这和<code>.filter()</code>方式是一样。如果该函数返回 <code>true</code>，那么当前元素就不会包含在结果中。
			</p>
</div>
<section class="entry-examples" id="entry-examples">
<header>
<h2 class="underline">例子:</h2>
</header>
<div class="entry-example" id="example-0">
<h4>Example: <span class="desc">为不是绿色或蓝色的 div 添加边框。</span> </h4>
<div class="syntaxhighlighter xml ">
<table>
<tbody>
<tr>
<td class="gutter"><div class="line n1">1</div>
<div class="line n2">2</div>
<div class="line n3">3</div>
<div class="line n4">4</div>
<div class="line n5">5</div>
<div class="line n6">6</div>
<div class="line n7">7</div>
<div class="line n8">8</div>
<div class="line n9">9</div>
<div class="line n10">10</div>
<div class="line n11">11</div>
<div class="line n12">12</div>
<div class="line n13">13</div>
<div class="line n14">14</div>
<div class="line n15">15</div>
<div class="line n16">16</div>
<div class="line n17">17</div>
<div class="line n18">18</div>
<div class="line n19">19</div>
<div class="line n20">20</div>
<div class="line n21">21</div>
<div class="line n22">22</div>
<div class="line n23">23</div>
<div class="line n24">24</div>
<div class="line n25">25</div>
<div class="line n26">26</div>
<div class="line n27">27</div>
<div class="line n28">28</div>
<div class="line n29">29</div>
<div class="line n30">30</div></td>
<td class="code"><pre><div class="container"><div class="line"><code><span class="doctype">&lt;!DOCTYPE html&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">html</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">head</span>&gt;</span></code></div></div><div class="container"><div class="line"><code> <span class="tag">&lt;<span class="title">style</span>&gt;</span><span class="css"></span></code></div></div><div class="container"><div class="line"><code><span class="tag">div</span> <span class="rules">{ <span class="rule"><span class="attribute">width</span>:<span class="value"><span class="number">50</span>px;</span></span> <span class="rule"><span class="attribute">height</span>:<span class="value"><span class="number">50</span>px;</span></span> <span class="rule"><span class="attribute">margin</span>:<span class="value"><span class="number">10</span>px;</span></span> <span class="rule"><span class="attribute">float</span>:<span class="value">left;</span></span></span></code></div></div><div class="container"><div class="line"><code> <span class="rule"><span class="attribute">background</span>:<span class="value">yellow;</span></span> <span class="rule"><span class="attribute">border</span>:<span class="value"><span class="number">2</span>px solid white;</span></span> <span class="rule">}</span></code></div></div><div class="container"><div class="line"><code><span class="class">.green</span> <span class="rules">{ <span class="rule"><span class="attribute">background</span>:<span class="value"><span class="hexcolor">#8f8</span>;</span></span> <span class="rule">}</span></span></code></div></div><div class="container"><div class="line"><code><span class="class">.gray</span> <span class="rules">{ <span class="rule"><span class="attribute">background</span>:<span class="value"><span class="hexcolor">#ccc</span>;</span></span> <span class="rule">}</span></span></code></div></div><div class="container"><div class="line"><code><span class="id">#blueone</span> <span class="rules">{ <span class="rule"><span class="attribute">background</span>:<span class="value"><span class="hexcolor">#99f</span>;</span></span> <span class="rule">}</span></span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;/<span class="title">style</span>&gt;</span></code></div></div><div class="container"><div class="line"><code> <span class="tag">&lt;<span class="title">script</span> <span class="attribute">src</span>=<span class="value">"https://code.jquery.com/jquery-latest.js"</span>&gt;</span><span class="javascript"></span><span class="tag">&lt;/<span class="title">script</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;/<span class="title">head</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">body</span>&gt;</span></code></div></div><div class="container"><div class="line"><code> <span class="tag">&lt;<span class="title">div</span>&gt;</span><span class="tag">&lt;/<span class="title">div</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">div</span> <span class="attribute">id</span>=<span class="value">"blueone"</span>&gt;</span><span class="tag">&lt;/<span class="title">div</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">div</span>&gt;</span><span class="tag">&lt;/<span class="title">div</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">div</span> <span class="attribute">class</span>=<span class="value">"green"</span>&gt;</span><span class="tag">&lt;/<span class="title">div</span>&gt;</span></code></div></div><div class="container"><div class="line"><code> </code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">div</span> <span class="attribute">class</span>=<span class="value">"green"</span>&gt;</span><span class="tag">&lt;/<span class="title">div</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">div</span> <span class="attribute">class</span>=<span class="value">"gray"</span>&gt;</span><span class="tag">&lt;/<span class="title">div</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">div</span>&gt;</span><span class="tag">&lt;/<span class="title">div</span>&gt;</span></code></div></div><div class="container"><div class="line"><code> </code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">script</span>&gt;</span><span class="javascript"></span></code></div></div><div class="container"><div class="line"><code>$(<span class="string">"div"</span>).not(<span class="string">".green, #blueone"</span>)</code></div></div><div class="container"><div class="line"><code> .css(<span class="string">"border-color"</span>, <span class="string">"red"</span>);</code></div></div><div class="container"><div class="line"><code> </code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;/<span class="title">script</span>&gt;</span></code></div></div><div class="container"><div class="line"><code> </code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;/<span class="title">body</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;/<span class="title">html</span>&gt;</span></code></div></div>
			</pre></td>
</tr>
</tbody>
</table>
</div>
<h4>Demo:</h4>
<div class="demo code-demo"></div>
</div>
<div class="entry-example" id="example-1">
<h4>Example: <span class="desc">从段落集合中移除 ID 是 "selected" 的元素。</span> </h4>
<div class="syntaxhighlighter javascript ">
<table>
<tbody>
<tr>
<td class="gutter"><div class="line n1">1</div></td>
<td class="code"><pre><div class="container"><div class="line"><code>$(<span class="string">"p"</span>).not( $(<span class="string">"#selected"</span>)[<span class="number">0</span>] )</code></div></div>
			</pre></td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="entry-example" id="example-2">
<h4>Example: <span class="desc">从段落集合中移除 ID 是 "selected" 的元素。</span> </h4>
<div class="syntaxhighlighter javascript ">
<table>
<tbody>
<tr>
<td class="gutter"><div class="line n1">1</div></td>
<td class="code"><pre><div class="container"><div class="line"><code>$(<span class="string">"p"</span>).not(<span class="string">"#selected"</span>)</code></div></div>
			</pre></td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="entry-example" id="example-3">
<h4>Example: <span class="desc">从段落集合中移除满足 "div p.selected" 的元素。</span> </h4>
<div class="syntaxhighlighter javascript ">
<table>
<tbody>
<tr>
<td class="gutter"><div class="line n1">1</div></td>
<td class="code"><pre><div class="container"><div class="line"><code>$(<span class="string">"p"</span>).not($(<span class="string">"div p.selected"</span>))</code></div></div>
			</pre></td>
</tr>
</tbody>
</table>
</div>
</div>
</section>
</div>
</article>
</div>
</article>